using System;
using System.Collections.Generic;
using System.Web.Mvc;
using MvcMall.Core.Contacts.Tasks;
using MvcMall.Framework.Extensions;

namespace MvcMall.Portal.Controllers.Home
{
    public class HomeController : Controller
    {
        private readonly IGoodsCategoryTask _goodsCategoryTask;

        public HomeController(IGoodsCategoryTask goodsCategoryTask)
        {
            _goodsCategoryTask = goodsCategoryTask;
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult ShowCate()
        {
            return View();
        }

        public ActionResult AllCate()
        {
            var list = _goodsCategoryTask.GetByGrade(1).ToSelectList(c => c.Id.ToString(), c => c.CatName);
            var list1 = new List<SelectListItem> { };
            var list2 = new List<SelectListItem> { };
            var list3 = new List<SelectListItem> { };
            var list4 = new List<SelectListItem> { };
            var list5 = new List<SelectListItem> { };
            var list6 = new List<SelectListItem> { };
            var list7 = new List<SelectListItem> { };
            var list8 = new List<SelectListItem> { };
            var list9 = new List<SelectListItem> { };
            var list10 = new List<SelectListItem> { };
            var list11 = new List<SelectListItem> { };
            foreach (var item in (list as IEnumerable<SelectListItem>))
            {
                var goodsCat = _goodsCategoryTask.Get(Convert.ToInt32((item.Value).ToInt()));
                if (goodsCat == null)
                    return new EmptyResult();
                var sublist = goodsCat.ChildCats.ToSelectList(c => c.Id.ToString(), c => c.CatName);

                ViewData["subList" + item.Value] = sublist;
                int catId = Convert.ToInt16((item.Value).ToInt());
                if (catId >= 1 && catId <= 3)
                {
                    list1.Add(new SelectListItem {Text = item.Text, Value = item.Value});
                } 
                else if (catId >= 4 && catId <= 11)
                {
                    list2.Add(new SelectListItem { Text = item.Text, Value = item.Value });
                }
                else if (catId >= 12 && catId <= 14)
                {
                    list3.Add(new SelectListItem { Text = item.Text, Value = item.Value });
                }
                else if (catId >= 15 && catId <= 38)
                {
                    list4.Add(new SelectListItem { Text = item.Text, Value = item.Value });
                }
                else if (catId >= 39 && catId <= 49)
                {
                    list5.Add(new SelectListItem { Text = item.Text, Value = item.Value });
                }
                else if (catId >= 50 && catId <= 62)
                {
                    list6.Add(new SelectListItem { Text = item.Text, Value = item.Value });
                }
                else if (catId >= 63 && catId <= 67)
                {
                    list7.Add(new SelectListItem { Text = item.Text, Value = item.Value });
                }
                else if (catId >= 68 && catId <= 73)
                {
                    list8.Add(new SelectListItem { Text = item.Text, Value = item.Value });
                }
                else if (catId >= 74 && catId <= 77)
                {
                    list9.Add(new SelectListItem { Text = item.Text, Value = item.Value });
                }
                else if (catId >= 78 && catId <= 81)
                {
                    list10.Add(new SelectListItem {Text = item.Text, Value = item.Value});
                }
                else if (catId >= 82 && catId <= 84)
                {
                    list11.Add(new SelectListItem { Text = item.Text, Value = item.Value });
                }
            }
            ViewData["catList"] = list;
            ViewData["catList1"] = list1;
            ViewData["catList2"] = list2;
            ViewData["catList3"] = list3;
            ViewData["catList4"] = list4;
            ViewData["catList5"] = list5;
            ViewData["catList6"] = list6;
            ViewData["catList7"] = list7;
            ViewData["catList8"] = list8;
            ViewData["catList9"] = list9;
            ViewData["catList10"] = list10;
            ViewData["catList11"] = list11;
            return View();
        }

        public ActionResult Messages(MessageViewModel model)
        {
            return View(model);
        }
    }
}